<template>
    <div class="chose">
        <div class="table" v-if="active==1">
            <el-table :border="true" :data="pageResult.records">
                <el-table-column prop="id" v-if="false"></el-table-column>
                <el-table-column label="案号" prop="caseNo"></el-table-column>
                <el-table-column label="委托人" prop="caseWtr"></el-table-column>
                <el-table-column label="对方当事人" prop="caseOppositeParties"></el-table-column>
                <el-table-column label="代理费" prop="caseAgencyfee"></el-table-column>
                <el-table-column label="已付款" prop="casePaidsal"></el-table-column>
                <el-table-column label="已开票" prop="caseInvoiced"></el-table-column>
                <el-table-column label="收案日期" prop="collectionTime"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="handleEdit(scope.row)" v-if="scope.row.casePlan!='C'">修改</el-button>
                        <el-button type="text" @click="handleDelete(scope.row)" v-if="scope.row.casePlan!='B'">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-footer class="case-list-footer">
                <el-pagination
                        style="margin: 10px auto"
                        background
                        layout="prev, pager, next"
                        :total="pageResult.total"
                        :page-size="pageResult.size"
                        class="pagination"
                        :current-page="pageResult.current"
                        @current-change="handleIndexChange"
                >
                </el-pagination>
            </el-footer>
        </div>

        <div class="auto-box" v-if="active==2">
            <manager-case-update
                    :active="active"
                    :law="law"
                    @edit="handleSubmitEdit"
                    @back="handleBack"
                    :cwPerformancesplitList="cwPerformancesplitList"
            >
            </manager-case-update>
        </div>

        <div style="width: 100%;" v-if="active==3">
            <feiyong-add :law="law" @back="handleBack"></feiyong-add>
        </div>
    </div>
</template>

<script>
    //引入组件
    const ManagerCaseUpdate = () => import('./ManagerCaseUpdate.vue')
    const FeiyongAdd = () => import('../Feiyong/FeiyongAdd.vue')
    //引入服务类
    import CaseLawService from '../../../model/gs/CaseLawService.js'
    import {CwPerformancesplitService} from "../../../model/ww/cw_performancesplit/CwPerformancesplitService";

    const caseLawService = CaseLawService.getInstance()
    const cwPerformancesplitService = CwPerformancesplitService.getInstance()

    export default {
        data() {
            return {
                active:1,
                law:{},
                pageResult:[],
                params:{
                    recycle:'N'
                },
                //办案律师数组
                cwPerformancesplitList:[],
            }
        },
        methods:{
            //获取所有案件
            getCaseLaws(){
                caseLawService.getByPage(this.params).then(response => {
                    if(response.data.code === 0){
                        this.pageResult = response.data.data
                    }
                })
            },
            handleEdit(law){
                this.$axios.all([caseLawService.getById(law.id),
                                cwPerformancesplitService.getByCaseLaw(law.id)])
                    .then(this.$axios.spread((lawResponse,cwResponse) => {
                        this.law = lawResponse.data.data
                        this.cwPerformancesplitList = cwResponse.data.data
                        this.active++
                    }))
            },
            handleSubmitEdit(){
                //判断使用哪种校验方式
                const type = this.cwPerformancesplitList[0].type
                if(type == '1'){
                    if(!caseLawService.check(this,this.law,this.cwPerformancesplitList)){
                        return
                    }
                }else{
                    if(!caseLawService.zxdlCheck(this,this.law,this.cwPerformancesplitList)){
                        return
                    }
                }
                //发送请求
                caseLawService.updateWithlawyer(this.law,this.cwPerformancesplitList).then(response => {
                    if(response.data.code === 0){
                        //判断代理费是否低于最低标的
                        if(this.law.caseAgencyfee<this.law.caseLawsuitobj){
                            this.$message.warning({
                                message:'代理费低于最低标的，将进行费用减免申请',
                                duration:2000
                            })
                            this.active = 3
                        }else{
                            this.$message.success({
                                message:'修改成功',
                                duration:2000
                            })
                            //刷新数据
                            this.getCaseLaws()
                            this.active = 1
                        }
                    }
                })
            },
            handleDelete(law){
                law.caseRecycle = 'Y'
                caseLawService.update(law).then(response => {
                    if(response.data.code === 0){
                        //刷新数据
                        this.getCaseLaws()
                    }
                })
            },
            //当前页放生变化的事件
            handleIndexChange(index){
                this.params.page = index
                this.getCaseLaws()
            },
            handleBack(){
                this.active = 1
            }
        },
        created(){
            this.getCaseLaws()
        },
        components:{
            ManagerCaseUpdate,
            FeiyongAdd
        }
    }
</script>

<style scoped>
    .chose{
        width: 100%;
    }
    .case-list-footer{
        width: 100%;
        height: 10%;
        position: relative;
    }
    .case-list-footer .pagination{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
    }
</style>